package com.gurun.camera.device.presenter.impl;

import android.os.Handler;
import android.os.Looper;

import com.danale.player.entity.CloudRecordDevice;
import com.danale.player.entity.SdRecordDevice;
import com.danale.sdk.device.service.response.GetBaseInfoResponse;
import com.danale.sdk.platform.entity.device.Device;
import com.danale.sdk.platform.entity.v3.PushMsg;
import com.gurun.camera.device.bean.CloudRecordInfo;
import com.gurun.camera.device.constant.VideoDataType;
import com.gurun.camera.device.listener.OnCallbackListener;
import com.gurun.camera.device.model.ControlManager;
import com.gurun.camera.device.model.impl.CloudSDControlManager;
import com.gurun.camera.device.presenter.ICloudAndSDPresenter;
import com.gurun.camera.device.util.DateTimeUtil;
import com.gurun.camera.device.view.ICloudAndSdView;
import com.gurun.camera.device.view.ICloudSdPlayView;
import com.gurun.camera.device.view.ICloudView;
import com.gurun.camera.model.DeviceCloudInfo;
import com.gurun.camera.mvp.IBaseView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CloudAndSdPresenter implements ICloudAndSDPresenter, OnCallbackListener {

    ICloudAndSdView iCloudAndSdView;
    ICloudSdPlayView iCloudSdPlayView;
    ICloudView iCloudView;
    ControlManager controlManager;

    public CloudAndSdPresenter(IBaseView playView, VideoDataType type){
        if (playView instanceof  ICloudAndSdView){
            iCloudAndSdView = (ICloudAndSdView) playView;
        }
        if (playView instanceof ICloudView){
            iCloudView = (ICloudView) playView;
        }
        if (playView instanceof ICloudSdPlayView){
            iCloudSdPlayView = (ICloudSdPlayView) playView;
        }
        controlManager = new CloudSDControlManager(type);
        controlManager.setOnCallbackListener(this);
    }

    @Override
    public void setData(String id) {
        controlManager.setId(id);
    }

    @Override
    public void getSDState() {
        controlManager.getSdState();
    }

    @Override
    public void getSDRecordList(int year, int month, int day) {
        controlManager.getSDRecordList(DateTimeUtil.formatDate(year, month, day));
    }

    @Override
    public void getSDRecordList(int year, int month, int day, int channel) {
        controlManager.getSDRecordList(DateTimeUtil.formatDate(year, month, day), channel);
    }

    @Override
    public void getSDPlayerInfo(long timestamp) {
        controlManager.getSDRecordPlayInfo(timestamp);
    }

    @Override
    public void getCloudState() {
        controlManager.getCloudState();
    }

    @Override
    public void getCloudStateList(List<Device> devices) {
        controlManager.getCloudStateByList(devices);
    }

    @Override
    public void getCloudRecordList(long startTimestamp) {
        controlManager.getCloudRecordList(startTimestamp);
    }

    @Override
    public void getCloudPlayerInfo(long timestamp) {
        controlManager.getCloudRecordPlayInfo(timestamp);
    }

    @Override
    public void handleSelectDate(int year, int month, int day) {
        long timestamp = DateTimeUtil.formatDate(year, month, day);
        if (iCloudAndSdView != null){
            iCloudAndSdView.selectDateTime(timestamp);
        }
    }

    @Override
    public void getSDRecordList(long timestamp, int channel) {
        controlManager.getSDRecordList(timestamp, channel);
    }

    @Override
    public void getSDPlayerInfo(long timestamp, int channel) {
        controlManager.getSDRecordPlayInfo(timestamp, channel);
    }

    @Override
    public void getCloudRecordList(long timestamp, int channel) {
        controlManager.getCloudRecordList(timestamp, channel);
    }

    @Override
    public void getCloudPlayerInfo(long timestamp, int channel) {
        controlManager.getCloudRecordPlayInfo(timestamp, channel);
    }

    @Override
    public void getCloudPlayerInfoByPushMsg(PushMsg pushMsg) {
        controlManager.getCloudRecordPlayInfoByPushMsg(pushMsg);
    }

    public void onSDStateCallback(final GetBaseInfoResponse response) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (iCloudAndSdView != null){
                    iCloudAndSdView.showSDState(response);
                }
            }
        });
    }

    @Override
    public void onCloudStateCallback(final DeviceCloudInfo info) {
        if (iCloudAndSdView != null){
            iCloudAndSdView.showCloudInfo(info);
        }
    }



    @Override
    public void onCloudStateListCallback(List<DeviceCloudInfo> deviceCloudInfoList) {
        if (iCloudView != null){

        }
        iCloudView.showCloudInfoList(deviceCloudInfoList);
    }

    @Override
    public void onRecordListCallback(final ArrayList<CloudRecordInfo> recordInfoList) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Collections.sort(recordInfoList, new Comparator<CloudRecordInfo>() {

                    @Override
                    public int compare(CloudRecordInfo lhs, CloudRecordInfo rhs) {
                        if(lhs.getStartTime() - rhs.getStartTime() <= 0){
                            return -1;
                        }else{

                            return 1;
                        }
                    }
                });
                if (iCloudAndSdView != null){
                    iCloudAndSdView.showRecordList(recordInfoList);
                }
            }
        });
    }

    @Override
    public void onCloudRecordStartCallback(boolean isStart, int code, CloudRecordDevice device) {
        if (iCloudSdPlayView != null){
            iCloudSdPlayView.handleCloudStartVideo(device, code);
        }
    }

    @Override
    public void onSDRecordStartCallback(final SdRecordDevice device) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (iCloudSdPlayView != null){
                    iCloudSdPlayView.handleSDStartVideo(device);
                }
            }
        });
    }

}
